package com.zg.datapush.web;

import cn.hutool.core.bean.BeanUtil;
import com.zg.common.core.R;
import com.zg.common.core.dao.datapush.entity.DpInterfaceConfig;
import com.zg.datapush.vo.DpInterfaceConfigVo;
import com.zg.datapush.req.*;
import com.zg.datapush.service.DpInterfaceConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 接口配置Controller
 * @author zg
 */
@RestController
@RequestMapping("/datapush/interfaceConfig")
@RequiredArgsConstructor
@Api(tags = "接口配置管理")
public class DpInterfaceConfigController {

    private final DpInterfaceConfigService interfaceConfigService;

    @ApiOperation("根据任务ID查询接口配置")
    @PostMapping("/get")
    public R<DpInterfaceConfigVo> getByTaskId(@RequestBody @Valid DpInterfaceConfigTaskIdReq req) {
        DpInterfaceConfigVo result = interfaceConfigService.getByTaskId(req.getTaskId());
        return R.success(result);
    }

    @ApiOperation("新增接口配置")
    @PostMapping("/add")
    public R<DpInterfaceConfig> add(@RequestBody @Valid DpInterfaceConfigAddReq req) {
        // 转换Req到Vo
        DpInterfaceConfigVo vo = new DpInterfaceConfigVo();
        BeanUtil.copyProperties(req, vo);

        return interfaceConfigService.add(vo);
    }

    @ApiOperation("更新接口配置")
    @PostMapping("/update")
    public R<DpInterfaceConfig> update(@RequestBody @Valid DpInterfaceConfigUpdateReq req) {
        return interfaceConfigService.update(req);
    }

    @ApiOperation("保存或更新接口配置")
    @PostMapping("/saveOrUpdate")
    public R<DpInterfaceConfig> saveOrUpdate(@RequestBody @Valid DpInterfaceConfigUpdateReq req) {
        return interfaceConfigService.saveOrUpdate(req);
    }

    @ApiOperation("删除接口配置")
    @PostMapping("/delete")
    public R<Void> deleteByTaskId(@RequestBody @Valid DpInterfaceConfigTaskIdReq req) {
        return interfaceConfigService.deleteByTaskId(req.getTaskId());
    }
}
